//
//  MSQueueImageView.m
//  Musou
//
//  Created by luo danal on 6/15/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MSQueueImageView.h"
#import "MSHTTPRequest.h"


@implementation MSQueueImageView


static NSOperationQueue *__sharedQueue = nil;
+ (id)sharedQueue{
    @synchronized(self){
        if (__sharedQueue == nil) {
            __sharedQueue = [[NSOperationQueue alloc] init];  
        } 
        return __sharedQueue;
    }
}

+ (void)setMaxConcurrent:(NSInteger)num{
    [[self sharedQueue] setMaxConcurrentOperationCount:num];
}

#pragma mark - Override

- (void)setUrl:(NSString *)url{
    if (_url != url) {
        [_url release];
        _url = [url copy];
        NSString *fileName = [self cacheName];
        if (self.useThumbnail) {
            fileName = [[self class] thumbnailCacheNameForUrl:self.url];
        }
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        path = [path stringByAppendingPathComponent:fileName];
        NSFileManager *fm = [NSFileManager defaultManager];
        if ([fm fileExistsAtPath:path]) {
            if (self.style == kMSImageViewStyleProgressBar) {
                _progressbar.hidden = YES;
            }
            self.image = [UIImage imageWithContentsOfFile:path];
        } else {
            if (self.style == kMSImageViewStyleProgressBar) {
                _progressbar.hidden = NO;
            } else {
                [_indicator startAnimating];
            }
            self.image = self.placeholder == nil ? nil : self.placeholder;
            //Use a shared pool to limit concurrent connections
            
//            NSURL *URL = [NSURL URLWithString:_url];
//            NSURLRequest *req = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.f];
//            [NSURLConnection connectionWithRequest:req delegate:self];
        }
        
    }

}

@end
